package com.paziy.jan.university.persons;

public class Name {

    private final int hashCode;

    private final String surname;
    private final String firstName;
    private final String lastName;

    /**
     * Create a Name object which consist of apart surname, firs and last name
     * @param surname surname of person
     * @param firstName first name of person
     * @param lastName last name of person
     */
    public Name(String surname, String firstName, String lastName) {
        this.surname = surname;
        this.firstName = firstName;
        this.lastName = lastName;

        hashCode = getFullName().hashCode();
    }

    public String getSurname() {
        return surname;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    /**
     * Return surname + first name + last name
     * @return full name of person
     */
    public String getFullName() {
        return surname + " " + firstName + " " + lastName;
    }

    /**
     * Return short name of person surname + first character of first name + first character of last name
     * @return short name of person
     */
    public String getShortName() {
        return surname + " " + firstName.substring(0,1) + "." + lastName.substring(0,1);
    }

    /**
     * Result of method getShortName
     * @return short name of person
     * @see Name#getShortName()
     */
    @Override
    public String toString() {
        return  getShortName();
    }

    @Override
    public int hashCode() {
        return hashCode;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;

        if (obj == null) return false;

        if (this.getClass() != obj.getClass()) return false;

        Name other = (Name) obj;

        if (this.surname.equals(other.surname)) return false;

        if (this.firstName.equals(other.firstName)) return false;

        if (this.lastName.equals(other.lastName)) return false;

        return true;
    }
}